1 Load packages and filtering

library(ggsci)
library(haven)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
AQ <- read_sav("C:/Users/Colin/Documents/GitHub/Colin_portfolio/p03/data/AQ.sav")

AQf <- AQ %>% 
  filter(Asian == "1")

#donut noninteractive

freq_table(AQf, Race)
## # A tibble: 7 × 3
##   Race                            n  prop
##   <chr>                       <int> <dbl>
## 1 ""                             28  15.4
## 2 "East"                         72  39.6
## 3 "Filipino/Pacific Islander"    25  13.7
## 4 "Multiethnic"                   8   4.4
## 5 "Multiracial"                   7   3.8
## 6 "South Asian"                  11   6  
## 7 "Southeast"                    31  17
donut <- data.frame(
  Race=c("East", "Southeast", "Filipino/Pacific Islander",  "South Asian", "Multiethnic", "Multiracial"),
  count=c(72, 31, 25, 11, 8, 7)
)

donut$fraction <- donut$count / sum(donut$count)

# Compute the cumulative percentages (top of each rectangle)
donut$ymax <- cumsum(donut$fraction)

# Compute the bottom of each rectangle
donut$ymin <- c(0, head(donut$ymax, n=-1))

# Compute label position
donut$labelPosition <- (donut$ymax + donut$ymin) / 2

# Compute a good label
donut$label <- paste0(donut$category, "\n value: ", donut$count)


#donut
ggplot(donut, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=Race)) +
  geom_rect() +
  scale_fill_jco()  + 
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() + ggtitle("Asian Ethnicity")

#bar interactive

p <- ggplot(donut, aes(x = Race, y = count, fill=Race)) +
  geom_col() + scale_fill_jco()  + theme_void() + ggtitle("Asian Ethnicity")

ggplotly(p)

#pie interactive

colors <- c('#0073C2FF',    '#EFC000FF',    '#868686FF',    '#CD534CFF',    
'#7AA6DCFF', '#003C67FF',   '#8F7700FF',    '#3B3B3BFF', '#A73030FF',   '#4A6990FF')



fig <- plot_ly(donut, labels = ~Race, values = ~count, type = 'pie', marker = list(colors = colors,
                      line = list(color = '#FFFFFF', width = 1)))

fig <- fig %>% layout(title = 'Asian Ethnicity')

fig